package com.itheima.springboot_19_cache.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.springboot_19_cache.dao.BookDao;
import com.itheima.springboot_19_cache.domain.Book;
import com.itheima.springboot_19_cache.service.IBookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl2 extends ServiceImpl<BookDao, Book> implements IBookService {
    @Autowired
    private BookDao bookDao;
    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        //System.out.println(currentPage+pageSize);
        IPage page=new Page(currentPage,pageSize);
        bookDao.selectPage(page,null);
        return page;
    }
    @Override
    public IPage<Book> getPage(int currentPage, int pageSize,String name) {
        LambdaQueryWrapper <Book> lqw=new LambdaQueryWrapper <Book> ();
        //lqw.like(String.isNotEmpty(book.getType()),Book::getType,book.getType());
        //lqw.like(String.isNotEmpty(book.getName()),Book::getName,book.getName());
        lqw.like(Strings.isNotEmpty(name),Book::getName,name);//Book::getName是数据对象对应的字段
        //lqw.like(String.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription());
        //System.out.println(currentPage+pageSize);
        IPage page=new Page(currentPage,pageSize);//page依赖MPConfig
        bookDao.selectPage(page,lqw);//后面是查询条件
        return page;
    }

    @Override
    @Cacheable(value="cacheSpace",key="#id") //表明这个查询用内存缓存,缓存变量是cacheSpace,缓存查询关键字是id,注意要有#号
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    @Override
    @Cacheable(value="cacheSpace",key="#id2") //表明这个查询用内存缓存,缓存变量是cacheSpace,缓存查询关键字是id2,注意要有#号
    public Integer testSetStringCache(Integer id,Integer id2,Integer id3) {
        return id;
    }
    //注意key一样,那么值类型要一样,否则新值无法替换老值,且程序会报错
    //还有个问题,我存在redis中的数据是Unicode的,程序员存进去的是明文,这是为什么?
}
